package com.gitee.qdbp.general.common.biz.sms.service;

import javax.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.gitee.qdbp.able.exception.ServiceException;
import com.gitee.qdbp.general.common.api.sms.model.ISmsTemplate;
import com.gitee.qdbp.general.common.biz.sms.utils.DefSmsBusiness;
import com.gitee.qdbp.tools.utils.Config;

/**
 * 空短信接口, 什么也不干, 只输出日志
 *
 * @author zhaohuihua
 * @version 160109
 */
// @Service // 这个Service在spring的xml文件中配置, 因为有些平台不需要短信功能
public class NullSmsService extends BaseSmsService {

    private static final Logger log = LoggerFactory.getLogger(NullSmsService.class);

    @PostConstruct
    public void init() {
        this.config = new Config();
        this.config.put("time.interval", "60"); // 两次同类型短信间隔时间
        this.config.put("time.cache", "5 * 60"); // 验证码5分钟之内有效
        if (utils instanceof Config.Aware) {
            ((Config.Aware) utils).setConfig(config);
        }
        this.setBaseBusiness(new DefSmsBusiness());
    }

    @Override
    public String send(String phone, ISmsTemplate template, Object... args) throws ServiceException {

        utils.check(phone, template.name());

        StringBuilder text = new StringBuilder();
        for (int i = 0; i < args.length; i++) {
            if (i > 0) {
                text.append(",");
            }
            text.append("{").append(i + 1).append("}");
            text.append("=").append(args[i]);
        }

        log.info("给[{}]发送[{}]短信成功, 参数[{}]", phone, template, text);

        return null;
    }

    @Override
    public Double balances() throws ServiceException {
        double number = 234.56D;
        log.info("获取短信余额成功, 余额[{}]", number);
        return number;
    }

}
